package lmh.example.exam6_1;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.TextView;

import java.util.Iterator;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //设置全屏显示
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //获取显示LocationProvider名称的TextView组件
        TextView textView = (TextView) findViewById(R.id.provider);
        //获取location服务
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        int permission = ActivityCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            // 请求权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        }

        //获取系统所有的LocationProvider名称
        List<String> providersNames = locationManager.getAllProviders();
        StringBuilder stringBuilder = new StringBuilder();//使用StringBuilder保存数据
        //遍历获取到的全部LocationProvider名称
        for (Iterator<String> iterator = providersNames.iterator(); iterator.hasNext(); ) {
            stringBuilder.append(iterator.next() + "\n");
        }
        textView.setText(stringBuilder.toString());  //显示LocationProvider名称

        //获取GPS LocationProvider
//        LocationProvider lp = locationManager.getProvider(LocationManager.GPS_PROVIDER);
//        textView.setText(lp.getName());
    }
}
